﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.Admin;
using BLL.Admin;

namespace WindowsForms.Admin
{
    public partial class UcTaiKhoan : UserControl
    {
        List<string> dl = new List<string>();
        int count = 0;
        public UcTaiKhoan()
        {
            InitializeComponent();
            datagrgiaidau.AutoGenerateColumns = false;
            datagrnhanvien.AutoGenerateColumns = false;
            datataikhoan.AutoGenerateColumns = false;


        }
        private void btnload_Click(object sender, EventArgs e)
        {
            LoadNhanVien(NhanVien.Objects.All().ToList());
            LoadGiaiDau(GiaiDau.Objects.All().Where(x=>x.ThoiGianKetThuc>=DateTime.Now).ToList());


        }

        private void LoadNhanVien(List<NhanVien> ds)
        {
            datagrnhanvien.DataSource = ds;
        }
        private void LoadGiaiDau(List<GiaiDau> ds)
        {
            datagrgiaidau.DataSource = ds;

        }

        private void datagrnhanvien_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex == 2)
            {

                for (int i = 0; i < datagrnhanvien.Rows.Count; i++)
                {
                    if (datagrnhanvien.Rows[i].Cells[2].Value != null && datagrnhanvien.Rows[i].Cells[2].Value.ToString() == "true" && i != e.RowIndex)
                        datagrnhanvien.Rows[i].Cells[2].Value = false;
                }





            }
        }


        private void datagrgiaidau_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                for (int j = 0; j < datagrgiaidau.Rows.Count; j++)
                {
                    if (datagrgiaidau.Rows[j].Cells[3].Value != null && datagrgiaidau.Rows[j].Cells[3].Value.ToString() == "true" && j != e.RowIndex)
                        datagrgiaidau.Rows[j].Cells[3].Value = false;
                }
            }

        }
        private int HangChonDataNV()
        {
            for (int i = 0; i < datagrnhanvien.Rows.Count; i++)
            {
                if (datagrnhanvien.Rows[i].Cells[2] != null && (datagrnhanvien.Rows[i].Cells[2].Value + "").Trim() == "true")
                    return i;
            }
            return -1;
        }
        private int HangChonDataGD()
        {
            for (int i = 0; i < datagrgiaidau.Rows.Count; i++)
            {
                if (datagrgiaidau.Rows[i].Cells[3] != null && (datagrgiaidau.Rows[i].Cells[3].Value + "").Trim() == "true")
                    return i;
            }
            return -1;
        }

        private void btnadd_Click(object sender, EventArgs e)
        {

            int hangnv = HangChonDataNV();
            int hanggd = HangChonDataGD();
            if (hangnv == -1 || hanggd == -1)
                MessageBox.Show("Bạn Chưa Chọn Nhân Viên Hoặc Giải Đấu Vui Lòng Chọn Lại", "Thông Báo", MessageBoxButtons.OK);
            else
            {

                string manhanvien = datagrnhanvien.Rows[hangnv].Cells[0].Value + "";
                string tennv = datagrnhanvien.Rows[hangnv].Cells[1].Value + "";
                string tengd = datagrgiaidau.Rows[hanggd].Cells[1].Value + "";
                string magiaidau = datagrgiaidau.Rows[hanggd].Cells[0].Value + "";
                string ns = NhanVien.Objects.GetById(manhanvien).NamSinh.ToString("dd/MM/yy");
                string a = "";
                string[] k = ns.Split('/');
                foreach (var item in k)
                    a += item;




                if (count == 0)
                {
                    datataikhoan.Rows.Add(manhanvien + "GD" + magiaidau, a, "BP Thủ Quỷ", true, "[" + manhanvien + "] " + tennv, "[" + magiaidau + "]" + tengd);
                    count++;
                }
                else if (!dl.Contains(manhanvien + "GD" + magiaidau))
                {
                    datataikhoan.Rows.Add(manhanvien + "GD" + magiaidau, a, "BP Thủ Quỷ", true, "[" + manhanvien + "] " + tennv, "[" + magiaidau + "]" + tengd);
                }
                dl.Add(manhanvien + "GD" + magiaidau);



            }

        }

        private void datataikhoan_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 6)
            {
                try
                {
                    datataikhoan.Rows.RemoveAt(e.RowIndex);
                }
                catch
                {


                }

            }
            if (e.ColumnIndex == 7)
            {
                try
                {
                    int quyen = -1;
                    int i;
                    TaiKhoan tk = new TaiKhoan();
                    tk.UserName = datataikhoan[0, e.RowIndex].Value.ToString();
                    tk.Password = datataikhoan[1, e.RowIndex].Value.ToString();

                    for (i = 0; i < cbpq.Items.Count; i++)
                        if (datataikhoan[2, e.RowIndex].Value.ToString() == cbpq.Items[i].ToString())
                            quyen = i + 1;
                    tk.PhanQuyen = quyen;
                    string manv = datataikhoan[4, e.RowIndex].Value.ToString();
                    manv = manv.Substring(manv.IndexOf('[') + 1, manv.LastIndexOf(']') - 1);
                    string magd = datataikhoan[5, e.RowIndex].Value.ToString();
                    magd = magd.Substring(magd.IndexOf('[') + 1, magd.LastIndexOf(']') - 1);
                    tk.MaGiaiDau = Convert.ToInt32(magd);
                    tk.MaNhanVien = manv;
                    tk.TrangThai = Convert.ToBoolean(datataikhoan.Rows[e.RowIndex].Cells[3].Value);

                    // lôoi  day
                    if (!QuanLiTaiKhoan.KiemTraDuyNhat(tk.MaNhanVien, tk.MaGiaiDau, TaiKhoan.Objects.All().ToList()))
                    {
                        if (!QuanLiTaiKhoan.KiemTraMa(tk.UserName, TaiKhoan.Objects.All().ToList()))
                        {
                            bool kt = TaiKhoan.Objects.Add(tk);
                            if (kt)
                            {
                                datataikhoan.Rows.RemoveAt(e.RowIndex);
                            }
                        }
                        else MessageBox.Show("Trùng Tên Tài Khoản");
                    }
                    else MessageBox.Show("Nhân Viên Đã Có Tài Khoản Của Giải Đấu Rồi");
                }
                catch
                {


                }











            }




        }

        private void btndeletealltk_Click(object sender, EventArgs e)
        {
            datataikhoan.Rows.Clear();
            dl.Clear();
        }

        private void panelEx3_Click(object sender, EventArgs e)
        {

        }


    }
}
